﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ASPNET.StarterKit.BusinessLogicLayer;
using ASPNET.StarterKit.DataAccessLayer;
using System.Data.SqlClient;


public partial class InventoryTracker_TaxScheme : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnTaxSchemeNew.Enabled = true;
        btnTaxSchemeDelete.Enabled = false;
        btnTaxSchemeEdit.Enabled = false;
        btnTaxSchemeSubmit.Enabled = true;
        btnTaxSchemeUpdate.Enabled = false;
       
    }
        
    protected void btnTaxSchemeSubmit_Click(object sender, EventArgs e)
    {
        DataAccess DA= DataAccessHelper.GetDataAccess();
        TaxScheme aTaxScheme=new TaxScheme(txtTaxName.Text,float.Parse(txtTaxPercent.Text));
        try
        {
            if(DA.CreateTaxScheme(aTaxScheme))
            {
                lblResult.Text="Success";
            }
else
            {
                lblResult.Text="Failure";
            }
        }
        catch(Exception ex)
            {
                lblResult.Text = ex.Message.ToString();
            }
    }

    protected void DdlSelTaxID_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnTaxSchemeNew.Enabled = true;
        btnTaxSchemeDelete.Enabled = true;
        btnTaxSchemeEdit.Enabled = true;
        btnTaxSchemeSubmit.Enabled = false;
        btnTaxSchemeUpdate.Enabled = false;


        String SelectValue = DdlSelTaxID.SelectedValue;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "SELECT * FROM  TaxScheme WHERE  [TaxID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        DataTable dataTable = GetDataTable(cmd);

        txtTaxName.Text = dataTable.Rows[0]["TaxName"].ToString();
        txtTaxPercent.Text = dataTable.Rows[0]["TaxPercent"].ToString();
        con.Close();

    }

    public static DataTable GetDataTable(SqlCommand cmd)
    {

        SqlDataAdapter da = new SqlDataAdapter();
        //set the SelectCommand of the adapter
        da.SelectCommand = cmd;
        // create a new DataTable
        DataTable dtGet = new DataTable();
        //fill the DataTable
        da.Fill(dtGet);
        //return the DataTable
        return dtGet;

    }


    protected void btnTaxSchemeNew_Click(object sender, EventArgs e)
    {
        btnTaxSchemeNew.Enabled = false;
        btnTaxSchemeDelete.Enabled = false;
        btnTaxSchemeEdit.Enabled = false;
        btnTaxSchemeSubmit.Enabled = true;
        btnTaxSchemeUpdate.Enabled = false;

        txtTaxName.Text = "";
        txtTaxPercent.Text = "";

    }
    protected void btnTaxSchemeDelete_Click(object sender, EventArgs e)
    {

        String SelectValue = DdlSelTaxID.SelectedValue;

        btnTaxSchemeNew.Enabled = false;
        btnTaxSchemeDelete.Enabled = true;
        btnTaxSchemeEdit.Enabled = false;
        btnTaxSchemeSubmit.Enabled = false;
        btnTaxSchemeUpdate.Enabled = false;


        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "DELETE FROM  TaxScheme WHERE  [TaxID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtTaxName.Text = "";
        txtTaxPercent.Text = "";

    }
    protected void btnTaxSchemeEdit_Click(object sender, EventArgs e)
    {
        btnTaxSchemeNew.Enabled = false;
        btnTaxSchemeDelete.Enabled = false;
        btnTaxSchemeEdit.Enabled = false;
        btnTaxSchemeSubmit.Enabled = false;
        btnTaxSchemeUpdate.Enabled = true;

    }
    protected void btnSupUpdate_Click(object sender, EventArgs e)
    {
        btnTaxSchemeNew.Enabled = false;
        btnTaxSchemeDelete.Enabled = false;
        btnTaxSchemeEdit.Enabled = false;
        btnTaxSchemeSubmit.Enabled = false;
        btnTaxSchemeUpdate.Enabled = true;

        String SelectValue = DdlSelTaxID.SelectedValue;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "UPDATE TaxScheme SET  TaxName = '" + txtTaxName.Text + "' , TaxPercent = '" + txtTaxPercent.Text + "' WHERE [TaxID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();


        txtTaxName.Text = "";
        txtTaxPercent.Text = "";

    }
    
}
       
